package com.PVZ.model;

import com.PVZ.model.emuns.AnimeType;
import com.PVZ.model.emuns.MoveType;
import com.PVZ.model.interfaces.EventSource;
import com.PVZ.model.weapon.Weapon;
import com.PVZ.utils.Util;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.awt.*;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;

/**
 * Created by @Author: kabuda on @Date: 2021/5/17 23:05
 * <p>
 * 基础类
 */
@Getter
@Setter
@NoArgsConstructor
public abstract class Entity extends EventSource {


    public static int ALIVE = 2;
    public static int DEAD = 1;
    public static int CLEANED = 0;

    public String anime;

    /**
     * x y 的修正值
     * x值为负往左偏
     * y为负往上偏
     */
    private int reviseX;
    private int reviseY;

    //转换僵尸图片阈值
    private int transformZombies;
    //移动状态
    public MoveType moveType = MoveType.Stop;
    //移动速度
    private int curSpeed = 0;

    private int speed = 0;

    public Point point = new Point(0,0 );

    public int col = 0, row = 0;

    //当前贴图路径
    public String imgSrc;

    // private Image curImage;

    //当前动画
    private Map<AnimeType, Anime> animeMap;


    //打击音效
    public String hitSound;

    //死亡音效
    public Boolean hasPlaydeadSound = false;

    private Weapon weapon;

    /**
     * 当前正在播放动画的类型 默认为MOVE
     */
    private AnimeType curAnimeType = AnimeType.MOVE;

    public int health=1;
    /**
     * 是否存活 2:存活  1:死亡--未清除 0:需清除(从list中remove)
     */
    private int isAlive = ALIVE;

    /**
     * 设置动画
     */
    public abstract void setAnime();

    public Entity(MoveType moveType) {
        this.moveType = moveType;
        this.anime =UUID.randomUUID().toString();
        //设置好碰撞箱
        setBoxPadding(30, 30, 30, 30);
    }

    public Entity(Point point,MoveType moveType) {
        this.point = point;
        this.moveType = moveType;
        this.anime =UUID.randomUUID().toString();
        //设置好碰撞箱
        setBoxPadding(30, 30, 30, 30);

        this.row = Util.parseXtoRow(point.x);
        this.col = Util.parseYtoCol(point.y);
    }

    public Entity(int row, int col,MoveType moveType) {
        this.point = Util.parseRanksToPoint(row, col);
        this.moveType = moveType;
        this.anime = UUID.randomUUID().toString();
        this.row = row;
        this.col = col;
        //设置好碰撞箱
        setBoxPadding(30, 30, 30, 30);

    }

    public Entity(Point point, int reviseX, int reviseY, int speed) {
        this.point = point;
        this.reviseX = reviseX;
        this.reviseY = reviseY;
        this.speed = speed;
        this.curSpeed = speed;
        this.anime = UUID.randomUUID().toString();

        this.row = Util.parseXtoRow(point.x);
        this.col = Util.parseYtoCol(point.y);
        //设置好碰撞箱
        setBoxPadding(30, 30, 30, 30);
    }

    public Entity(int row, int col, int reviseX, int reviseY, int speed) {
        this.point = Util.parseRanksToPoint(row, col);
        this.reviseX = reviseX;
        this.reviseY = reviseY;
        this.speed = speed;
        this.curSpeed = speed;
        this.anime = UUID.randomUUID().toString();
        this.row = row;
        this.col = col;
        //设置好碰撞箱
        setBoxPadding(30, 30, 30, 30);

    }

    //碰撞箱左上点坐标以及宽度高度
    //boxLeftWidth--碰撞箱左边框距离图片坐标的宽度，另外几项也类似请在子类中设置好
    private int boxLeftWidth, boxLeftHeight, boxRightWidth, boxRightHeight;

    public void setBoxPadding(int boxLeftWidth, int boxLeftHeight, int boxRightWidth, int boxRightHeight) {
        this.boxLeftWidth = boxLeftWidth;
        this.boxLeftHeight = boxLeftHeight;
        this.boxRightWidth = boxRightWidth;
        this.boxRightHeight = boxRightHeight;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        if (!super.equals(o)) {
            return false;
        }
        Entity entity = (Entity) o;
        return anime.equals(entity.anime);
    }

    @Override
    public String toString() {
        return this.getClass().getSimpleName()+
                "{id='" + anime + '\'' +" moveType='"+this.moveType+ '\''+
                '}';
    }

    @Override
    public int hashCode() {
        return Objects.hash(super.hashCode(), anime);
    }

    public void stopMove() {
        curSpeed = 0;
    }

    /**
     * 获得计算后碰撞箱的x
     *
     * @return 左边
     */
    public int getBoxLeftPositionX() {
        return point.x +reviseX - boxLeftWidth;
    }

    /**
     * 获得计算后碰撞箱的x
     *
     * @return 右边
     */
    public int getBoxRightPositionX() {
        return point.x +reviseX + boxRightHeight;
    }

    /**
     * 获得计算后碰撞箱的y
     *
     * @return 上边
     */
    public int getBoxUpPositionY() {
        return point.y +reviseY - boxLeftHeight;
    }

    /**
     * 获得计算后碰撞箱的y
     *
     * @return 下边
     */
    public int getBoxDownPositionY() {
        return point.y +reviseY + boxRightHeight;
    }
}
